/*
 * Simple XDevice Status Query Tool
 * Uses simple Unix Domain Socket to query basic status
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>

#define STATUS_SOCKET_PATH "/tmp/xdevice_status.sock"

// Simple message protocol
typedef struct {
    int cmd;  // 1=status, 2=devices, 3=cluster
    char data[256];
} simple_msg_t;

typedef struct {
    int node_id;
    int cluster_size;
    int leader_id;
    uint64_t term;
    int device_count;
    char status[64];
} simple_status_t;

static int create_status_server(void) {
    int server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (server_fd < 0) {
        perror("socket");
        return -1;
    }
    
    unlink(STATUS_SOCKET_PATH);
    
    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, STATUS_SOCKET_PATH, sizeof(addr.sun_path) - 1);
    
    if (bind(server_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        perror("bind");
        close(server_fd);
        return -1;
    }
    
    if (listen(server_fd, 1) < 0) {
        perror("listen");
        close(server_fd);
        return -1;
    }
    
    printf("Status server listening on %s\n", STATUS_SOCKET_PATH);
    return server_fd;
}

static int query_status(void) {
    int client_fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (client_fd < 0) {
        perror("socket");
        return -1;
    }
    
    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, STATUS_SOCKET_PATH, sizeof(addr.sun_path) - 1);
    
    if (connect(client_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        perror("connect");
        close(client_fd);
        return -1;
    }
    
    simple_msg_t msg = {.cmd = 1};
    if (send(client_fd, &msg, sizeof(msg), 0) < 0) {
        perror("send");
        close(client_fd);
        return -1;
    }
    
    simple_status_t status;
    if (recv(client_fd, &status, sizeof(status), 0) < 0) {
        perror("recv");
        close(client_fd);
        return -1;
    }
    
    printf("XDevice Status:\n");
    printf("  Node ID: %d\n", status.node_id);
    printf("  Cluster Size: %d\n", status.cluster_size);
    printf("  Leader ID: %d\n", status.leader_id);
    printf("  Term: %lu\n", status.term);
    printf("  Device Count: %d\n", status.device_count);
    printf("  Status: %s\n", status.status);
    
    close(client_fd);
    return 0;
}

int main(int argc, char* argv[]) {
    if (argc > 1 && strcmp(argv[1], "query") == 0) {
        return query_status();
    }
    
    printf("Simple XDevice Status Tool\n");
    printf("Usage:\n");
    printf("  %s          - Start status server (for testing)\n", argv[0]);
    printf("  %s query    - Query status from running server\n", argv[0]);
    
    // Demo server mode
    int server_fd = create_status_server();
    if (server_fd < 0) {
        return 1;
    }
    
    printf("Demo mode: accepting one connection...\n");
    
    int client_fd = accept(server_fd, NULL, NULL);
    if (client_fd >= 0) {
        simple_msg_t msg;
        if (recv(client_fd, &msg, sizeof(msg), 0) > 0) {
            printf("Received query command: %d\n", msg.cmd);
            
            simple_status_t status = {
                .node_id = 1,
                .cluster_size = 3,
                .leader_id = 1,
                .term = 42,
                .device_count = 2
            };
            strcpy(status.status, "Running");
            
            send(client_fd, &status, sizeof(status), 0);
        }
        close(client_fd);
    }
    
    close(server_fd);
    unlink(STATUS_SOCKET_PATH);
    
    return 0;
}
